/*
 * To change this template, choose Tools | Templates
 * and open the template in the editor.
 */
package system.log;

import java.io.Serializable;
import java.sql.SQLException;
import java.text.SimpleDateFormat;
import java.util.*;
import javax.el.ELContext;
import javax.faces.bean.ManagedBean;
import javax.faces.bean.ViewScoped;
import javax.faces.context.FacesContext;
import system.general.Exc;
import system.general.Logged;
import system.general.Message;

/**
 *
 * @author gilberto
 */
@ManagedBean(name = "system_log")
@ViewScoped
public class Main extends Beans implements Serializable {
    //Variables de importación General

    Logged logged;
    private Message msg = new Message();
    //Variables de importación otros modulos
    //Variables del modulo
    private Find logFind = new Find();
    private List<Beans> logList = new ArrayList<>();
    //Variables locales
    private SimpleDateFormat sdf = new SimpleDateFormat("yyyy-MM-dd");
    private String sql;
    private Map<String, String> map = new HashMap();
    private Date fechaFinal = new java.util.Date();
    private String detalle = "";
    private String accion = "";
    private String where = "";
    private StringBuilder sb = new StringBuilder("");

    public Main() {
        ELContext elContext = FacesContext.getCurrentInstance().getELContext();
        logged = (Logged) FacesContext.getCurrentInstance().getApplication().getELResolver().getValue(elContext, null, "logged");
    }

    private void load() {
        try {
            //Verifica si hay un registro seleccionado
            if (logged.getDetailId() == 0) {
                return;
            }

            map.clear();
            map.put("id", String.valueOf(logged.getDetailId()));

            logList.clear();
            logList = logFind.byMap(map);

            setId(logList.get(0).getId());
            setPersona(logList.get(0).getPersona());
            setSql(logList.get(0).getSql());
            setFecha(logList.get(0).getFecha());
            logged.setMasterName(logList.get(0).getPersona());

        } catch (Exc ex) {
            msg.showMsg(namePackage(), "system", ex.getIdError());
        } catch (SQLException ex) {
            msg.showErr(namePackage(), "sql", ex);
        } catch (Exception ex) {
            msg.showErr(namePackage(), "java", ex);
        } finally {
        }
    }

    public List<Beans> getBeans() {
        return logList;
    }

    public void find() {
        try {
            if (getFecha().after(getFechaFinal())) {
                throw new Exc("m20");
            }
            if (!getPersona().equals("")) {
                if (sb.length() != 0) {
                    sb.append(" AND");
                }
                sb.append(" persona = '").append(getPersona()).append("'");
            }
            if (!getAccion().equals("NULL")) {
                if (sb.length() != 0) {
                    sb.append(" AND");
                }
                sb.append(" sql like '%").append(getAccion()).append("%'");
            }
            if (!getDetalle().equals("")) {
                if (sb.length() != 0) {
                    sb.append(" AND");
                }
                sb.append(" sql like '%").append(getDetalle()).append("%'");
            }
            if (!getFechaFinal().toString().equals("") && !getFecha().toString().equals("")) {
                if (sb.length() != 0) {
                    sb.append(" AND");
                }
                sb.append(" fecha BETWEEN '").append(sdf.format(getFecha())).append("' AND '").append(sdf.format(getFechaFinal())).append("'");
            }

            if (sb.length() != 0) {
                where = "WHERE ";
            }

            sql = "SELECT * FROM public.v_bitacora " + where + sb;
            sb = new StringBuilder("");
            logList = logFind.executeSql(sql);

        } catch (Exc ex) {
            msg.showMsg(namePackage(), "system", ex.getIdError());
            // } catch (SQLException ex) {
            //   msg.showErr("sql", ex);
        } catch (Exception ex) {
            msg.showErr(namePackage(), "java", ex);
        } finally {
        }
    }

    public void clear() {
        clearBeans();
    }

    //SETTERS AND GETTERS
    /**
     * @return the fechaFinal
     */
    public Date getFechaFinal() {
        return fechaFinal;
    }

    /**
     * @param fechaFinal the fechaFinal to set
     */
    public void setFechaFinal(Date fechaFinal) {
        this.fechaFinal = fechaFinal;
    }

    /**
     * @return the detalle
     */
    public String getDetalle() {
        return detalle;
    }

    /**
     * @param detalle the detalle to set
     */
    public void setDetalle(String detalle) {
        this.detalle = detalle;
    }

    /**
     * @return the accion
     */
    public String getAccion() {
        return accion;
    }

    /**
     * @param accion the accion to set
     */
    public void setAccion(String accion) {
        this.accion = accion;
    }

    private String namePackage() {
        Package pack = this.getClass().getPackage();
        return pack.getName();
    }
}
